<template>
  <div class="container">
    <div class="search-box">
      <el-input
        placeholder="请输入内容"
        class="input-box"
        v-model="searchInfo"
      ></el-input>
      <el-button @click="clearInfo">清除</el-button>
      <el-button type="primary" @click="queryList">搜索</el-button>
    </div>
    <el-table border style="width: 100%" :data="dataList">
      <el-table-column type="index" label="任务编号" width="80">
      </el-table-column>
      <el-table-column prop="name" label="任务名称"> </el-table-column>
      <el-table-column prop="des" label="任务描述"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template #default="{ row }">
          <el-button
            size="mini"
            style="margin-right: 10px"
            @click="editDialog(row.id)"
            >编辑</el-button
          >
          <el-popconfirm
            confirmButtonText="好的"
            cancelButtonText="不用了"
            icon="el-icon-info"
            iconColor="red"
            title="确定删除任务吗？"
            @confirm="delConfirm(row.id)"
          >
            <template #reference>
              <el-button size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="pageContainer">
      <el-pagination
        layout="prev, pager, next"
        :total="total"
        :page-size="query._limit"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
      <el-button size="small" plain @click="showaddData">+ 新增</el-button>
    </div>

    <!-- 新增对话框 -->
    <el-dialog
      :title="form.id ? '更新' : '新增'"
      :visible="addFormdialog"
      @close="closedialog"
    >
      <el-form :model="form" :rules="rules" ref="addRef">
        <el-form-item label="任务名称" label-width="80px" prop="name">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="任务描述" label-width="80px" prop="des">
          <el-input v-model="form.des" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closedialog">取 消</el-button>
        <el-button type="primary" @click="addData">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getListAPI,
  addListAPI,
  getDetailAPI,
  searchListAPI,
  editListAPI,
  delListAPI,
} from "./api/request";
import { v4 as uuidv4 } from "uuid";
uuidv4();
export default {
  data() {
    return {
      dataList: [],
      query: {
        _page: 1, // 当前第几页
        _limit: 5, // 每页条数
      },
      total: 0,
      addFormdialog: false,
      // 增加数据
      form: {
        name: "",
        des: "",
        id: "",
      },
      rules: {
        name: [
          { required: true, message: "请输入任务名称", trigger: "blur" },
          { min: 1, max: 10, message: "长度在 1到 10 个字符", trigger: "blur" },
        ],
        des: [
          {
            required: true,
            message: "请输入任务描述",
            trigger: ["blur", "change"],
          },
          {
            min: 5,
            max: 25,
            message: "长度在 5 到 25 个字符",
            trigger: ["blur", "change"],
          },
        ],
      },
      editId: null,
      searchInfo: "",
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    async getList() {
      const res = await getListAPI(this.query);
      console.log(res);
      this.dataList = res.data;
      this.total = +res.headers["x-total-count"];
    },
    // 新增
    showaddData() {
      this.addFormdialog = true;
    },
    closedialog() {
      this.addFormdialog = false;
      this.$refs.addRef.resetFields();
      this.form = {
        name: "",
        des: "",
        id: "",
      };
    },
    async addData() {
      this.$refs.addRef.validate(async (valid) => {
        if (!valid) return;

        if (this.form.id) {
          await editListAPI(this.form);
        } else {
          let id = uuidv4();
          this.form.id = id;
          await addListAPI(this.form);
        }

        this.$message.success(`${this.form.id ? "更新成功" : "新增成功"}`);
        this.getList();
        this.closedialog();
        this.form = {
          name: "",
          des: "",
          id: "",
        };
      });
    },
    // 编辑
    async editDialog(id) {
      this.addFormdialog = true;
      this.editId = id;
      const res = await getDetailAPI(id);
      console.log(res);
      this.form = res.data;
    },
    // 分页
    handleCurrentChange(val) {
      this.query._page = val;
      this.getList();
    },
    // 删除
    async delConfirm(id) {
      await delListAPI(id);
      this.$message.success("删除成功");
      this.getList();
    },
    // 清除
    clearInfo() {
      this.searchInfo = "";
      this.getList();
    },
    // 搜索
    async queryList() {
      // 手动
      if (this.searchInfo === "") {
        this.getList();
        return;
      }
      const res = await searchListAPI(this.searchInfo);
      this.dataList = res.data;
      this.total = this.dataList.length;
    },
  },
};
</script>

<style scoped lang="less">
.container {
  width: 800px;
  margin: 0 auto;
}
.search-box {
  display: flex;
  margin-bottom: 10px;
  .input-box {
    margin-right: 20px;
  }
}
.pageContainer {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
}
</style>
